Skip to content

Use calendar.timegm instead of time.mktime for UTC timestamps #565

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

fenuks
Copy link

@fenuks fenuks commented May 8, 2025

time.mktime() converts a time tuple in local time to seconds since the Epoch, as stated in the docs:

Convert a time tuple in local time to seconds since the Epoch.
Note that mktime(gmtime(0)) will not generally return zero for most
time zones; instead the returned value will either be equal to that
of the timezone or altzone attributes on the time module.

calendar.timegm() is guaranteed to produce UTC timestamp:

Unrelated but handy function to calculate Unix timestamp from GMT.

time.mktime() converts a time tuple in local time to seconds since the
Epoch, as stated in the docs:

> Convert a time tuple in local time to seconds since the Epoch.
> Note that mktime(gmtime(0)) will not generally return zero for most
> time zones; instead the returned value will either be equal to that
> of the timezone or altzone attributes on the time module.

calendar.timegm() is guaranteed to produce UTC timestamp:

> Unrelated but handy function to calculate Unix timestamp from GMT.
@sirosen
Copy link
Member

sirosen commented May 15, 2025

Thanks for this fix!

I took some time to poke around and see if there's any other interface which provides this -- it feels weird to import calendar for basic time functionality -- but it looks like not. 🤷

I need to add a test which demonstrates the issue before I can merge, and there are issues in CI related to the lastest click release. I've got a PR up for the click issue, and that should unblock me working up a test to confirm this fix. 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants